package com.fy.module.qu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fy.common.consts.QuAnswerConstant;
import com.fy.common.consts.QuTypeConstant;
import com.fy.common.enums.ApiErrorEnum;
import com.fy.common.exception.ServiceException;
import com.fy.common.vo.ApiRest;
import com.fy.module.qu.dto.QuSaveDto;
import com.fy.module.qu.entity.Qu;
import com.fy.module.qu.entity.QuAnswer;
import com.fy.module.qu.entity.QuRepo;
import com.fy.module.qu.mapper.QuAnswerMapper;
import com.fy.module.qu.mapper.QuMapper;
import com.fy.module.qu.mapper.QuRepoMapper;
import com.fy.module.qu.service.IQuService;
import com.fy.module.repo.mapper.RepoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 问题题目 服务实现类
 * </p>
 *
 * @author 虚拟宇宙公司
 * @since 2023-03-21
 */
@Service
@Slf4j
public class QuServiceImpl extends ServiceImpl<QuMapper, Qu> implements IQuService {
    @Autowired
    private QuAnswerMapper quAnswerMapper;

    @Autowired
    private QuRepoMapper quRepoMapper;

    @Autowired
    private RepoMapper repoMapper;

    @Override
    @Transactional
    public ApiRest add(QuSaveDto quSaveDto) {
        //1、参数校验
        checkData(quSaveDto);

        //2、题目添加
        //2.1、保存qu
        Qu qu = new Qu();
        qu.setId(IdWorker.getIdStr());
        //hutool工具包提供方法，将参数1的属性复制给参数2，参数3:是否忽略名字不同的属性
        BeanUtil.copyProperties(quSaveDto, qu, true);
        qu.setCreateTime(new Date());
        qu.setUpdateTime(new Date());
//        qu.setAnalysis(quSaveDto.getAnalysis());
//        qu.setContent(quSaveDto.getContent());
//        qu.setImage(quSaveDto.getImage());
//        qu.setQuType(quSaveDto.getQuType());
//        qu.setRemark(quSaveDto.getRemark());
        //baseMapper相当于注入了QuMapper,然后调用QuMapper
        this.baseMapper.insert(qu);

        //2.2、保存答案选项
        List<QuAnswer> answers = quSaveDto.getAnswers();
        for (QuAnswer answer : answers) {
            answer.setId(IdWorker.getIdStr());
            answer.setQuId(qu.getId());
            quAnswerMapper.insert(answer);
        }

        //2.3、关联题目和题库
        String[] repoIds = quSaveDto.getRepoIds();
        for (String repoId : repoIds) {
            QuRepo quRepo = new QuRepo();
            quRepo.setId(IdWorker.getIdStr());
            quRepo.setQuType(qu.getQuType());
            quRepo.setQuId(qu.getId());
            quRepo.setRepoId(repoId);
            quRepoMapper.insert(quRepo);

            //更新题库的题目数
            repoMapper.updateRepoQuCount(repoId);
        }

        log.info("{}-{}添加题目成功", "cxk", new Date());
        return ApiRest.success();
    }

    /**
     * 参数校验
     * @param qu
     */
    private void checkData(QuSaveDto qu) {

        if (StringUtils.isEmpty(qu.getContent())) {
            throw new ServiceException(ApiErrorEnum.QU_CONTENT_IS_NULL);
        }


        if (CollectionUtils.isEmpty(Arrays.asList(qu.getRepoIds()))) {
            throw new ServiceException(ApiErrorEnum.QU_REPO_ATLEAST_ONE);
        }

        List<QuAnswer> answers = qu.getAnswers();


        if (qu.getQuType() != QuTypeConstant.JDT && CollectionUtils.isEmpty(answers)) {
            throw new ServiceException(ApiErrorEnum.QU_HAS_ANSWER);
        }


        int trueCount = 0;
        for (QuAnswer a : answers) {

            if (StringUtils.isEmpty(a.getContent())) {
                throw new ServiceException(ApiErrorEnum.ANS_HAS_CONTENT);
            }

            if (a.getIsRight() == QuAnswerConstant.RIGHT) {
                trueCount += 1;
            }
        }

        if (trueCount == 0) {
            throw new ServiceException(ApiErrorEnum.QU_HAS_RIGHT_ANSWER);
        }


        //单选题
        if (qu.getQuType() == QuTypeConstant.RADIO && trueCount > 1) {
            throw new ServiceException(ApiErrorEnum.QU_RADIO_HAS_MULTI_RIGHT_ANSWER);
        }

        //判断题
        if (qu.getQuType() == QuTypeConstant.JUDGE && trueCount > 1) {
            throw new ServiceException(ApiErrorEnum.QU_RADIO_HAS_MULTI_RIGHT_ANSWER);
        }

        //多选题
        if (qu.getQuType() == QuTypeConstant.MULTI && trueCount <= 1) {
            throw new ServiceException(ApiErrorEnum.QU_MULTI_HAS_MULTI_TRUE_ANSWER);
        }

    }
}
